/**
 * 
 */
package jm.lib.web.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jm.lib.common.db.ConditionOperation;
import jm.lib.util.StrUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author JimingLiu
 * 
 */
public class HtmlUtil {
	private static final Log logger = LogFactory.getLog(HtmlUtil.class);
	
	private static String NON_VALUE_CONDITION_OPERATION_OPTION;
	
	static {
	   NON_VALUE_CONDITION_OPERATION_OPTION = getConditionOperationOptionsCommon(null, false);
  }
	
	public static String getConditionOperationOptions(String value) {
	   boolean hasValue = StrUtil.isNotEmpty(value);
	   return hasValue?getConditionOperationOptionsCommon(value, true)
	           :NON_VALUE_CONDITION_OPERATION_OPTION;
	}
	
	/**
	 * 
	 * @param value
	 * @return
	 */
	private static String getConditionOperationOptionsCommon(String value, boolean hasValue) {
		StringBuilder sb = new StringBuilder();
		ConditionOperation[] cos = ConditionOperation.values();
		for (ConditionOperation co : cos) {
			String name = encodeHtml(co.getName());
			sb.append("<option value=\"").append(name);
			sb.append("\"");
			if (hasValue && co.getName().equalsIgnoreCase(value)) {
				sb.append(" selected=\"true\"");
			}
			sb.append(">");
			sb.append(name);
			sb.append("</option>\n");
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * @param name
	 * @param list
	 * @param value
	 * @return
	 */
	public static String getSelectList(String name, List list, String value) {
		StringBuilder sb = new StringBuilder();
		getSelectList(name, list, value, sb);
		return sb.toString();
	}
	
	public static void getSelectList(String name, List list, String value, StringBuilder sb) {
		boolean hasValue = StrUtil.isNotEmpty(value);
		sb.append("<select name=\"").append(name).append("\" size=\"1\">\n");
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			String element = iter.next().toString();
			String encode_element = encodeHtml(element);
			sb.append("<option value=\"").append(encode_element).append("\"");
			if(hasValue && element.equalsIgnoreCase(value)) {
				sb.append(" selected=\"true\"");
			}
			if (encode_element.equals("-1")) {
				encode_element = "ȫ��";
			}
			sb.append(">").append(encode_element).append("</option>\n");
		}
		sb.append("</select>\n");
	}
	
	/**
	 * 
	 * @param name
	 * @param map key is value of select list and value is label of select list 
	 * @param selected_value
	 * @return
	 */
	public static String getSelectList(String name, Map<String,String> map, String selected_value) {
		StringBuilder sb = new StringBuilder();
		getSelectList(name, map, selected_value, sb);
		return sb.toString();
		
	}
	
	/**
	 * 
	 * @param name
	 * @param map
	 * @param selected_value
	 * @param sb
	 */
	public static void getSelectList(String name, Map<String,String> map, String selected_value, StringBuilder sb) {
		boolean hasValue = StrUtil.isNotEmpty(selected_value);
		sb.append("<select name=\"").append(name).append("\" size=\"1\">\n");
		for(Map.Entry<String,String> entry: map.entrySet()) {
			String value = encodeHtml(entry.getKey());
			String label = encodeHtml(entry.getValue());
			if(StrUtil.isEmpty(label)) {
				label = value;
			}
			sb.append("<option value=\"").append(value).append("\"");
			if(hasValue && selected_value.equalsIgnoreCase(entry.getKey())) {
				sb.append(" selected=\"true\"");
			}
			sb.append(">").append(label).append("</option>\n");
		}
		sb.append("</select>\n");
	}

	/**
	 * Please do not use it to encode non-ascii character
	 * @param value
	 * @return
	 */
	public static String encodeHtml(String value) {
		StringBuilder sb = new StringBuilder();
		int len = value.length();
		for (int i = 0; i < len; i++) {
			char c = value.charAt(i);
			switch (c) {
			case '&':
				sb.append("&amp;");
				break;
			case '<':
				sb.append("&lt;");
				break;
			case '>':
				sb.append("&gt;");
				break;
			case '"':
				sb.append("&quot;");
				break;
			default:
				sb.append(c);
				break;
			}
		}
		return sb.toString();
	}
}
